﻿using HalconDotNet;
using HWindow_Tool.Control;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace LD.Vision
{
    public partial class FrmVisionMain : DockContent
    {
        private FrmVisionMain()
        {
            InitializeComponent();

            this.Shown += FrmVisionMain_Shown;
            Init();
        }
        private static FrmVisionMain _instance;
        public static FrmVisionMain Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new FrmVisionMain();
                return _instance;
            }
        }
        private void FrmVisionMain_Shown(object sender, EventArgs e)
        {
        }

        public void Init()
        {
            switch (CameraFactory.Instance.CameraList.Count)
            {
                case 1:
                    Camer1();
                    break;
                case 2:
                    Camer2();
                    break;
                case 3:
                    break;
                case 4:
                    break;
                default:
                    break;
            }

        }
        private void Camer()
        {
            this.tabLayout.Visible = true;
            tabLayout.Dock = DockStyle.Fill;
            int row = 0;
            int column = 0;
            foreach (var item in CameraFactory.Instance.CameraList)
            {
                var window = GetHWindow(item.Key);
                this.tabLayout.Controls.Add(window, row, column);
                column++;
            }
            this.Controls.Add(tabLayout);
        }
        public void Camer1()
        {
            var camera = CameraFactory.Instance.CameraList.FirstOrDefault();
            var window = GetHWindow(camera.Key);
            this.tabLayout.Controls.Add(window);
            this.tabLayout.Visible = true;
            tabLayout.Dock = DockStyle.Fill;
        }
        public void Camer2()
        {
            this.tabLayout.Dock = DockStyle.Fill;
            this.tabLayout.Visible = true;
            int row = 0;
            int column = 0;
            foreach (var item in CameraFactory.Instance.CameraList)
            {
                var window = GetHWindow(item.Key);
                this.tabLayout.Controls.Add(window, column, row);
                column++;
            }
            this.Controls.Add(tabLayout);
        }
        public Dictionary<string, HWindow_Final> HWindow { get; private set; } = new Dictionary<string, HWindow_Final>();
        //private HWindow_Final GetHWindow(int index)
        //{
        //    var final = CameraFactory.CameraList[index + ""].HWindow;
        //    final.Dock = DockStyle.Fill;
        //    final.Margin = new Padding(8, 8, 8, 8);
        //    final.TabIndex = 0;
        //    final.hWindowControl.Tag = index;
        //    //注册窗口鼠标事件
        //    //final.hWindowControl.MouseDown += HWindowControl_MouseDown; ;
        //    return final;
        //}
        private HWindow_Final GetHWindow(string index)
        {
            var final = new HWindow_Final();// VisionFactory.Instance.HWindows[index].getHWindowControl();
            final.Dock = DockStyle.Fill;
            final.Margin = new Padding(8, 8, 8, 8);
            final.TabIndex = 0;

            HWindow.Add(index, final);
            final.hWindowControl.Tag = index;
            //注册窗口鼠标事件
            //final.hWindowControl.MouseDown += HWindowControl_MouseDown; ;
            return final;
        }
        private bool draw = false;//是否要绘制
        private int style = 0;//要绘制的类型0--直线，1圆，2，矩形，3，角度矩形，4，扇形

        private void HWindowControl_MouseDown(object sender, MouseEventArgs e)
        {
            var hWindow = sender as HWindowControl;
            var fWindow = hWindow.Parent as HWindow_Final;
            if (hWindow == null || fWindow == null)
                return;

            int index;

            List<HTuple> data;
            ViewWindow.Model.ROI roi = fWindow.viewWindow.smallestActiveROI(out data, out index);//可以获取最后一次选中或者编辑对象的数据

            if (index > -1)
            {
                string name = roi.GetType().Name;//得到最后一次选中的对象名字
                //this.dgv_ROI.Rows[index].Cells[0].Selected = true;
                //this.regions[index] = roi;//这里更新了数据---这句话非常重要
                CameraFactory.Instance.CameraList[hWindow.Tag.ToString()].Roi[index] = roi;
            }

            if (draw == false) return;
            hWindow.HalconWindow.GetMposition(out int y, out int x, out int b);

            //  this.Text = b.ToString();
            if (b == 4)
            {
                draw = false;
                return;//按下的是右键
            }
            List<ViewWindow.Model.ROI> regions = null;

            switch (style)
            {
                case 0://dian
                    fWindow.viewWindow.genPoint(y, x, ref regions);
                    break;

                case 1://直线
                    fWindow.viewWindow.genLine(y, x - 20, y, x + 20, ref regions);
                    break;

                case 2://圆
                    fWindow.viewWindow.genCircle(y, x, 60.0, ref regions);
                    break;
                case 3://矩形
                       // hWindow_Fit1.viewWindow.genLine(y, x - 20, y, x + 20, ref this.region1s);
                    fWindow.viewWindow.genRect1(y - 50, x - 50, y + 50, x + 50, ref regions);
                    break;
                case 4://角度矩形
                    fWindow.viewWindow.genRect2(y, x, 30.0 / 180.0 * Math.PI, 60.0, 30.0, ref regions);
                    break;
                case 5://扇形
                    fWindow.viewWindow.genCircularArc(y, x, 40.0, 0, -3.14, "negative", ref regions);
                    break;
                default:
                    draw = false;//退出
                    return;

            }

            regions.Last().Color = "blue";
            //binDataGridView(this.dgv_ROI, this.regions);
            draw = false;
            CameraFactory.Instance.CameraList[hWindow.Tag.ToString()].Roi = regions;
        }
    }
}
